package com.ars.order;

import com.ars.db.DBTools;
import com.ars.main.StageCollection;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import javax.swing.*;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.ResourceBundle;

public class QueryController {
    @FXML
    private TextField tfAddressFrom;
    @FXML
    private TextField tfAddressTo;
    @FXML
    private DatePicker dpDate;

    @FXML
    public void queryAction() {
        if (tfAddressFrom.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "请填写出发地", "警告", JOptionPane.ERROR_MESSAGE);
        } else if (tfAddressTo.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "请填写目的地", "警告", JOptionPane.ERROR_MESSAGE);
        } else if (dpDate.getValue() == null) {
            JOptionPane.showMessageDialog(null, "请填写出发时间", "警告", JOptionPane.ERROR_MESSAGE);
        } else if (dpDate.getValue().isBefore(LocalDate.now())) {
            JOptionPane.showMessageDialog(null, "不可购买过期机票！", "警告", JOptionPane.ERROR_MESSAGE);
        } else if (getPreFid().size() == 0) {
            JOptionPane.showMessageDialog(null, "改日票已售空，或则无该日航班！");
        } else {
            gotoQueryResult();
        }
    }

    //跳转到查询结果页面
    private void gotoQueryResult() {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("QueryResult.fxml"));
        Parent root = null;

        loader.setResources(new ResourceBundle() {
            @Override
            protected Object handleGetObject(String key) {
                if (key.equals("Fids")) {
                    return getPreFid();
                }
                return null;
            }

            @Override
            public Enumeration<String> getKeys() {
                return null;
            }
        });
        try {
            root = loader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Stage stage = new Stage();
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.setTitle("查询结果");
        stage.show();
        StageCollection.getInstance().put("queryResultStage", stage);
    }

    //获取符合条件的航班id
    private ArrayList<Integer> getPreFid() {
        ArrayList<Integer> list = new ArrayList<>();
        String sql = "SELECT fid from flight,schedule WHERE flight.sid = schedule.sid and address_from = '" + tfAddressFrom.getText() + "' and address_to = '" + tfAddressTo.getText() + "'";
        ResultSet rs = new DBTools().query(sql);
        try {
            while (rs.next()) {
                list.add(rs.getInt("fid"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
